// app.js
const tools = require('/lib/utils/tools');

App({
  onLaunch() {
    this.methods.initData.call(this);
    // 测试环境
    // wx.cloud.init({
    //   env: 'test-0gek6bcw7d5d6b14'
    // });
    // 生产环境
    wx.cloud.init({
      env: 'production-3gjvaiisa4e784cf'
    });
  },

  methods: {
    ...tools,
    initData () {
      this.methods.initTasks.call(this);
      this.methods.initDailyTasks.call(this);
      this.methods.initTags.call(this);
      this.methods.initSetting.call(this);
      this.methods.initStatistics.call(this);
      this.methods.initOperateLead.call(this);
    },
    initTasks () {
      let tasks = wx.getStorageSync('tasks');

      if (tasks) {
        tasks = JSON.parse(tasks);
        this.methods.isObject(tasks) && (this.globalData.tasks = tasks);
      }
    },
    initDailyTasks () {
      let daily = wx.getStorageSync('daily');

      if (daily) {
        daily = JSON.parse(daily);
        this.methods.isObject(daily) && (this.globalData.daily = daily);
      }
    },
    initTags () {
      let tags =  wx.getStorageSync('tags');

      if (tags) {
        tags = JSON.parse(tags);
        Array.isArray(tags) && (this.globalData.tags = tags);
      }
    },
    initSetting () {
      let setting = wx.getStorageSync('setting');

      if (setting) {
        setting = JSON.parse(setting);
        this.methods.isObject(setting) && (this.globalData.setting = setting);
      }
    },
    initStatistics () {
      // 初始化数据
      let statistics =  wx.getStorageSync('statistics');

      if (statistics) {
        statistics = JSON.parse(statistics);
        this.methods.isObject(statistics) && (this.globalData.statistics = statistics);
      }
      // 数据监听
      this.methods.observer.call(this, this.globalData.statistics, (value) => {
        wx.setStorageSync('statistics', JSON.stringify(this.globalData.statistics));
      });
    },
    initOperateLead () {
      let operateLead = wx.getStorageSync('operate-lead');

      if (operateLead) {
        operateLead = JSON.parse(operateLead);
        this.methods.isObject(operateLead) && (this.globalData.operateLead = operateLead);
      }
      // 数据监听
      this.methods.observer.call(this, this.globalData.operateLead, (value) => {
        wx.setStorageSync('operate-lead', JSON.stringify(this.globalData.operateLead));
      });
    }
  },
  globalData: {
    userInfo: null,
    prioritys: [
      {
        id: 'priority_1',
        title: '重要且紧急'
      },
      {
        id: 'priority_2',
        title: '重要不紧急'
      },
      {
        id: 'priority_3',
        title: '紧急不重要'
      },
      {
        id: 'priority_4',
        title: '不紧急不重要'
      }
    ], 
    // 默认任务对象
    tasks: {
      priority_1: {
        id: 'priority_1',
        title: '重要且紧急',
        className: 'priority-1',
        height: 0, // 该模块的section__list高度
        list: [
          {
            belong: "priority_1",
            content: "任务象限-任务示例",
            datetime: "2021-06-11 00:00",
            disable: true,
            id: "cfunkqxsxr",
            remind: "true",
            tag: "社交",
            x: 0,
            y: 90
          },
          {
            belong: "priority_1",
            content: "知鸟考试",
            datetime: "",
            disable: true,
            id: "xym4mapp4h",
            remind: "true",
            tag: "",
            x: 0,
            y: 200
          },
          {
            belong: "priority_1",
            content: "项目上线",
            datetime: "",
            disable: true,
            id: "i0ru_gv_re",
            remind: "true",
            tag: "工作",
            x: 0,
            y: 310
          }
        ] // 任务列表
      },
      priority_2: {
        id: 'priority_2',
        title: '重要不紧急',
        className: 'priority-2',
        height: 0,
        list: [
          {
            belong: "priority_2",
            content: "妈妈生日",
            datetime: "2021-09-13 15:00",
            disable: true,
            id: "hioj07s6cj",
            remind: "true",
            tag: "家庭",
            x: 0,
            y: 540
          },
          {
            belong: "priority_2",
            content: "考驾照",
            datetime: "2021-08-11 00:00",
            disable: true,
            id: "qwi621r7e6",
            remind: "true",
            tag: "学习",
            x: 0,
            y: 650
          }
        ]
      },
      priority_3: {
        id: 'priority_3',
        title: '紧急不重要',
        className: 'priority-3',
        height: 0,
        list: [
          {
            belong: "priority_3",
            content: "钥匙掉了，再配一把",
            datetime: "",
            disable: true,
            id: "4nws0zhb5.",
            remind: "true",
            tag: "",
            x: 0,
            y: 880
          },
          {
            belong: "priority_3",
            content: "5公里慢跑",
            datetime: "",
            disable: true,
            id: "k_izyy.tt_",
            remind: "true",
            tag: "运动",
            x: 0,
            y: 990
          }
        ]
      },
      priority_4: {
        id: 'priority_4',
        title: '不紧急不重要',
        className: 'priority-4',
        height: 0,
        list: [
          {
            belong: "priority_4",
            content: "同学聚会",
            datetime: "2021-07-14 00:00",
            disable: true,
            id: "dhh39f2ueg",
            remind: "true",
            tag: "社交",
            x: 0,
            y: 1220
          }
        ]
      },
    },
    // 默认日常任务对象
    daily: {
      priority_1: {
        id: 'priority_1',
        title: '任务列表',
        className: 'priority-1',
        height: 0, // 该模块的section__list高度
        list: [
          {
            belong: "priority_1",
            content: "每日任务-任务示例",
            datetime: "",
            disable: true,
            id: "c.3d9n4m4u",
            remind: false,
            tag: "",
            x: 0,
            y: 90
          },
          {
            belong: "priority_1",
            content: "早起后慢跑20分钟",
            datetime: "07:00",
            disable: true,
            id: "pdkccppuiv",
            remind: "true",
            tag: "运动",
            x: 0,
            y: 90
          },
          {
            belong: "priority_1",
            content: "处理工作中的事务",
            datetime: "09:00",
            disable: true,
            id: "x596_xs3ny",
            remind: "true",
            tag: "工作",
            x: 0,
            y: 200
          },
          {
            belong: "priority_1",
            content: "午休前看看书，然后休息",
            datetime: "12:00",
            disable: true,
            id: "kgl.z6s3vw",
            remind: "true",
            tag: "",
            x: 0,
            y: 310
          },
          {
            belong: "priority_1",
            content: "处理工作事务",
            datetime: "14:00",
            disable: true,
            id: "6mkv9eazu4",
            remind: "true",
            tag: "工作",
            x: 0,
            y: 420
          },
          {
            belong: "priority_1",
            content: "放松一会，下楼喝奶茶",
            datetime: "16:00",
            disable: true,
            id: "pqx4zjwe7h",
            remind: "true",
            tag: "社交",
            x: 0,
            y: 530
          },
          {
            belong: "priority_1",
            content: "睡前阅读 三国演义",
            datetime: "22:00",
            disable: true,
            id: "d8oi5e_edz",
            remind: "true",
            tag: "学习",
            x: 0,
            y: 640
          }
        ] // 任务列表
      }
    },
    // 默认tag
    tags: [
      {
        id: '.5hhbexb6y',
        label: '工作',
        icon: '/lib/images/tag-work.png'
      },
      {
        id: 'hs2hd7udrf',
        label: '学习',
        icon: '/lib/images/tag-study.png'
      },
      {
        id: '8dknz5374v',
        label: '家庭',
        icon: '/lib/images/tag-home.png'
      },
      {
        id: '4eol0lqoy6',
        label: '运动',
        icon: '/lib/images/tag-sport.png'
      },
      {
        id: 'lwqyfmfk8f',
        label: '社交',
        icon: '/lib/images/tag-social.png'
      }
    ],
    // 默认设置
    setting: {
      tomatoTopSound: true, // 番茄闹钟提示音
      isScreenLongActive: true
    },
     // 默认数据统计对象
    statistics: {
      tasksTotal: 0,
      finishedTasksTotal: 0,
      clockTimes: 0,
      clockTotalTime: 0
    },
    operateLead: {
      tasks_idea: true,
      tasks_create: true,
      tasks_task_item: true,
      daily_idea: true,
      clock_work_process: true
    }
  }
})
